package com.lxy.demo.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.net.URLDecoder;

@WebServlet(name = "GetCookieServlet", urlPatterns = "/GetCookieServlet")
public class GetCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取浏览器的cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            //URLDecoder.decode()解决中文乱码
            System.out.println(cookie.getName()+"="+ URLDecoder.decode(cookie.getValue(),"utf-8"));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
